/**
 * Created by 耀保
 *
 * 数据字典工具类
 */
import store from '@/store'

export const DICT_TYPE = {
    USER_TYPE: 'user_type',
    COMMON_STATUS: 'common_status',
    TERMINAL: 'terminal',
    //SA
    CUSTOMER_STATUS: 'customer_status',
    CUSTOMER_SOURCE: 'customer_source',
    QW_SCOPE: 'qw_scope',
    FUND_TYPE: 'fund_type',
    BOUND_TYPE: 'in_out_bound_type',
    WMS_RECEIPT_STATUS: 'wms_receipt_status',
    wms_receipt_type: 'wms_receipt_type', // 一级入库类型
    WMS_SHIPMENT_STATUS: 'wms_shipment_status',
    WMS_SHIPMENT_type: 'wms_shipment_type', // 一级出库类型
    FUND_COLLECTION_TYPE: 'fund_collection_type', //资金收款类型
    COLLECTION_TYPE: 'collection_type', //资金收款类型
    PAYMENT_TYPE: 'payment_type', //资金付款类型
    FUND_PAYMENT_TYPE: 'fund_payment_type', //资金付款类型
    COLLECTION_STATUS: 'collection_status', //收款状态
    PAYMENT_STATUS: 'payment_status', //付款状态
    sale_objectives_type: 'sale_objectives_type', //销售指标
    WRITE_OFF_TYPE: 'write_off_type',
    WRITE_OFF_BUSINESS_TYPE: 'write_off_business_type',
    WRITE_OFF_STATUS: 'write_off_status', // 核销状态
    RELATED_PARTY_TYPE: 'related_party_type', // 往来单位
    PWS_FOLLOW_UP_STAGE:'pws_follow_up_stage',//官网跟进状态

    // ========== SYSTEM 模块 ==========

    SYSTEM_USER_SEX: 'system_user_sex',
    SYSTEM_MENU_TYPE: 'system_menu_type',
    SYSTEM_ROLE_TYPE: 'system_role_type',
    SYSTEM_ROLE_DATA_SCOPE: 'system_role_data_scope',
    SYSTEM_DATA_SCOPE: 'system_data_scope',
    SYSTEM_NOTICE_TYPE: 'system_notice_type',
    SYSTEM_OPERATE_TYPE: 'system_operate_type',
    SYSTEM_LOGIN_TYPE: 'system_login_type',
    SYSTEM_LOGIN_RESULT: 'system_login_result',
    SYSTEM_SMS_CHANNEL_CODE: 'system_sms_channel_code',
    SYSTEM_SMS_TEMPLATE_TYPE: 'system_sms_template_type',
    SYSTEM_SMS_SEND_STATUS: 'system_sms_send_status',
    SYSTEM_SMS_RECEIVE_STATUS: 'system_sms_receive_status',
    SYSTEM_ERROR_CODE_TYPE: 'system_error_code_type',
    SYSTEM_OAUTH2_GRANT_TYPE: 'system_oauth2_grant_type',
    SYSTEM_MAIL_SEND_STATUS: 'system_mail_send_status',
    SYSTEM_NOTIFY_TEMPLATE_TYPE: 'system_notify_template_type',

    // ========== INFRA 模块 ==========
    INFRA_BOOLEAN_STRING: 'infra_boolean_string',
    INFRA_REDIS_TIMEOUT_TYPE: 'infra_redis_timeout_type',
    INFRA_JOB_STATUS: 'infra_job_status',
    INFRA_JOB_LOG_STATUS: 'infra_job_log_status',
    INFRA_API_ERROR_LOG_PROCESS_STATUS: 'infra_api_error_log_process_status',
    INFRA_CONFIG_TYPE: 'infra_config_type',
    INFRA_CODEGEN_TEMPLATE_TYPE: 'infra_codegen_template_type',
    INFRA_CODEGEN_FRONT_TYPE: 'infra_codegen_front_type',
    INFRA_CODEGEN_SCENE: 'infra_codegen_scene',
    INFRA_FILE_STORAGE: 'infra_file_storage',

    // ========== BPM 模块 ==========
    BPM_MODEL_CATEGORY: 'bpm_model_category',
    BPM_MODEL_FORM_TYPE: 'bpm_model_form_type',
    BPM_TASK_ASSIGN_RULE_TYPE: 'bpm_task_assign_rule_type',
    BPM_PROCESS_INSTANCE_STATUS: 'bpm_process_instance_status',
    BPM_PROCESS_INSTANCE_RESULT: 'bpm_process_instance_result',
    BPM_TASK_ASSIGN_SCRIPT: 'bpm_task_assign_script',

    // ========== MALL - PRODUCT 模块 ==========
    PRODUCT_SPU_STATUS: 'product_spu_status', // 产品 SPU 状态

    ACCOUNT_PERIOD_SALE_TYPE: 'account_period_sale_type', // 销售订单账期类型
    ACCOUNT_PERIOD_PROCURE_TYPE: 'account_period_procure_type', // 采购单账期类型

    // 合同
    CONTRACT_TYPE: 'contract_type', // 合同类型

    // 开票
    INVOICE_TYPE: 'invoice_type', // 开票类型
    INVOICE_STATUS: 'invoice_status', // 开票状态

    // 发货 模块
    DELIVER_TYPE: 'deliver_type' // 货物类型
}

/**
 * 获取 dictType 对应的数据字典数组
 *
 * @param dictType 数据类型
 * @returns {*|Array} 数据字典数组
 */
export function getDictDatas(dictType) {
    return store.getters.dict_datas[dictType] || []
}

/**
 * 获取 dictType 对应的数据字典数组
 *
 * @param dictType 数据类型
 * @param values 数组、单个元素
 * @returns {*|Array} 数据字典数组
 */
export function getDictDatas2(dictType, values) {
    if (values === undefined) {
        return []
    }
    // 如果是单个元素，则转换成数组
    if (!Array.isArray(values)) {
        values = [this.value]
    }
    // 获得字典数据
    const results = []
    for (const value of values) {
        const dict = getDictData(dictType, value)
        if (dict) {
            results.push(dict)
        }
    }
    return results
}

export function getDictData(dictType, value) {
    // 获取 dictType 对应的数据字典数组
    const dictDatas = getDictDatas(dictType)
    if (!dictDatas || dictDatas.length === 0) {
        return ''
    }
    // 获取 value 对应的展示名
    value = value + '' // 强制转换成字符串，因为 DictData 小类数值，是字符串
    for (const dictData of dictDatas) {
        if (dictData.value === value) {
            return dictData
        }
    }
    return undefined
}

export function getDictDataLabel(dictType, value) {
    const dict = getDictData(dictType, value)
    return dict ? dict.label : ''
}

export const processDefine = {
    调拨审批: 'inventory_movement_order',
    入库审批: 'inventory_receipt_order',
    出库审批: 'inventory_shipment_order',
    付款审批: 'fund_payment_order',
    收款审批: 'fund_collection_order',
    采购退货审批: 'procure_return_order',
    采购审批: 'procure_order',
    开票审批: 'invoice',
    销售退货审批: 'sale_return_order',
    订单审批: 'sale_order',
    合同审批: 'contract',
    产品审批: 'product',
    客户分配审批: 'customer_assign',
    客户回收审批: 'customer_recycle'
}
